package com.andreiamariei.tema6.services;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @author andrei.amariei, 24.11.2010 04:16:31
 *
 */
public class ForumService {
	private static ForumService instance = null;
	
	public static synchronized ForumService GetInstance() {
		if (instance == null) {
			instance = new ForumService();
		}
		return instance;
	}
	
	public String[] getPosts() {
		
		String[] posts = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			String url = "jdbc:mysql://localhost/tema6";
		    Connection conn = DriverManager.getConnection(url, "root", "root");
		    
		    String query = "SELECT TEXT FROM POSTS";
		    Statement st = conn.createStatement();
		    ResultSet rs = st.executeQuery(query);
		    
		    rs.last();
		    posts = new String[rs.getRow()];
		    rs.beforeFirst();
		    
		    while (rs.next())
	        {
	          posts[rs.getRow() - 1] = rs.getString("TEXT");
	        }
		    
		    conn.close();
		} catch (SQLException ignored) {
		} catch (ClassNotFoundException ignored) {
		}
	    
		return posts;
		
	}
}
